<?php
if( !defined( 'FLASH' ) ): define( 'FLASH', 1 );
if( !defined( 'FILE_ROOT' ) ) include_once( '../../_prerun' );

class Flash
	{
	static function header()
		{
		header( 'Content-type: text/plain;charset=utf-8' );
		error_reporting( E_ALL );
		set_error_handler ( 'flash_error' );
		}
		
	static function error( $msg )
		{
		die( '&com_status=0&error=' . self::encode( $msg ) );
		}
		
	static function progress( $progress, $params = null )
		{
		if( $progress > 100 ) $progress = 10;
		$uparams = '';
		if( is_array( $params ) )
			{
			$prefix = '&';
			foreach( $params as $index => $value )
				{
				$uparams = $prefix . $index . '=' . encode( $value );
				}
			}
		else
			$uparams = ( isset( $params ) ) ? '&'.$params : '';
		die( '&com_status=1&progress_indicator=' . $progress.$uparams );
		}
		
	static function end( $params = null )
		{
		$uparams = '';
		if( is_array( $params ) )
			{
			$prefix = '&';
			foreach( $params as $index => $value )
				{
				$uparams = $prefix . $index . '=' . encode( $value );
				}
			}
		else
			$uparams = ( isset( $params ) ) ? '&'.$params : '';
		die( '&com_status=2&progress_indicator=100' . $uparams );
		}
		
	static function encode( $text )
		{
		return urlencode( utf8_encode( $text ) );
		}
	}

function flash_error( $errno, $errstr , $errfile , $errline )
	{
	$errfile = str_ireplace( FILE_ROOT, '/', $errfile );
	$html =<<<HTML
	line $errline of $errfile : $errstr
HTML;
	Flash::error( $html );
	}

endif;
?>